<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>商品列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="../static/TG_logo.jpg" type="image/x-icon" />
    <link rel="stylesheet" href="../static/css/font.css">
    <link rel="stylesheet" href="../static/css/xadmin.css">
    <script src="../static/js/jquery.min.js"></script>
    <script type="text/javascript" src="../static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
    <div class="x-body layui-anim layui-anim-up">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>商品标题
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="title" required lay-verify="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label">
                    <span class="x-red">*</span>商品分类
                </label>
                <div class="layui-input-inline">
                    <select name="contrller" class="valid">
                        <option value="0">请选择</option>
                        <option value="1">DNF</option>
                        <option value="2">源神</option>
                        <option value="3">王者荣耀</option>
                        <option value="4">英雄联盟</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>卖家名称
                </label>
                <div class="layui-input-inline">
                    <input name="sellername" required lay-verify="nikename" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="desc" class="layui-form-label">
                    描述
                </label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" id="desc" name="desc" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>价格
                </label>
                <div class="layui-input-inline">
                    <input name="price" required lay-verify="price" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>数量
                </label>
                <div class="layui-input-inline">
                    <input name="quantity" required lay-verify="number" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <a class="layui-btn" lay-filter="add" lay-submit="" onclick="add_cmd()">
                    增加
                </a>
            </div>
        </form>
    </div>
 <script src="../static/js/jquery.min.js"></script>
<script>
        var user = window.localStorage.getItem('TGshop_admin')
        layui.use(['form', 'layer'], function () {
            $ = layui.jquery;
            var form = layui.form
                , layer = layui.layer;

            //自定义验证规则
            form.verify({
                nikename: function (value) {
                    if (!value) {
                        return '卖家名称不能为空';
                    }
                },
                price: function (value) {
                    if (!value) {
                        return '价格要填啊';
                    }
                },
            });
        })

        // 监听提交
        // form.on('submit(add)', function () {
            function add_cmd(){
            // console.log(data);
            var title = $("input[name='title']").val();
            var sort = $('select').val();
            var name = $("input[name='sellername']").val();
            var desc = $("textarea[name='desc']").val();
            var price = $("input[name='price']").val();
            var quantity = $("input[name='quantity']").val();
            var params = {
                "title": title,
                'sort': sort,
                'name': name,
                'desc': desc,
                'price': price,
                'quantity': quantity
            }
            console.log(params)
            $.ajax({
                type: 'post',
                url: 'http://127.0.0.1:8000/v1/TGadmin/commodity/'+user,
                data: JSON.stringify(params),
                dataType: 'json',
                contentType: 'application/json',
                beforeSend: function (request) {
                    request.setRequestHeader("authorization", localStorage.getItem('TGshop_token'))
                },
                success: function (data) {
                    console.log(data);
                    if (data.code == 403) {
                        alert('添加商品失败!')
                    }
                    if (data.code == 200) {
                        layer.alert("增加成功", { icon: 6 }, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    }
                }
            });
            return false;
        };

    </script>
</body>

</html>